{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Making File Names Unique"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getFolderNames"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #保证文件名唯一"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code> 的字符串数组 <code>names</code> 。你将会在文件系统中创建 <code>n</code> 个文件夹：在第 <code>i</code> 分钟，新建名为 <code>names[i]</code> 的文件夹。</p>\n",
    "\n",
    "<p>由于两个文件 <strong>不能</strong> 共享相同的文件名，因此如果新建文件夹使用的文件名已经被占用，系统会以 <code>(k)</code> 的形式为新文件夹的文件名添加后缀，其中 <code>k</code> 是能保证文件名唯一的 <strong>最小正整数</strong> 。</p>\n",
    "\n",
    "<p>返回长度为<em> <code>n</code></em> 的字符串数组，其中 <code>ans[i]</code> 是创建第 <code>i</code> 个文件夹时系统分配给该文件夹的实际名称。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [&quot;pes&quot;,&quot;fifa&quot;,&quot;gta&quot;,&quot;pes(2019)&quot;]\n",
    "<strong>输出：</strong>[&quot;pes&quot;,&quot;fifa&quot;,&quot;gta&quot;,&quot;pes(2019)&quot;]\n",
    "<strong>解释：</strong>文件系统将会这样创建文件名：\n",
    "&quot;pes&quot; --&gt; 之前未分配，仍为 &quot;pes&quot;\n",
    "&quot;fifa&quot; --&gt; 之前未分配，仍为 &quot;fifa&quot;\n",
    "&quot;gta&quot; --&gt; 之前未分配，仍为 &quot;gta&quot;\n",
    "&quot;pes(2019)&quot; --&gt; 之前未分配，仍为 &quot;pes(2019)&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [&quot;gta&quot;,&quot;gta(1)&quot;,&quot;gta&quot;,&quot;avalon&quot;]\n",
    "<strong>输出：</strong>[&quot;gta&quot;,&quot;gta(1)&quot;,&quot;gta(2)&quot;,&quot;avalon&quot;]\n",
    "<strong>解释：</strong>文件系统将会这样创建文件名：\n",
    "&quot;gta&quot; --&gt; 之前未分配，仍为 &quot;gta&quot;\n",
    "&quot;gta(1)&quot; --&gt; 之前未分配，仍为 &quot;gta(1)&quot;\n",
    "&quot;gta&quot; --&gt; 文件名被占用，系统为该名称添加后缀 (k)，由于 &quot;gta(1)&quot; 也被占用，所以 k = 2 。实际创建的文件名为 &quot;gta(2)&quot; 。\n",
    "&quot;avalon&quot; --&gt; 之前未分配，仍为 &quot;avalon&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [&quot;onepiece&quot;,&quot;onepiece(1)&quot;,&quot;onepiece(2)&quot;,&quot;onepiece(3)&quot;,&quot;onepiece&quot;]\n",
    "<strong>输出：</strong>[&quot;onepiece&quot;,&quot;onepiece(1)&quot;,&quot;onepiece(2)&quot;,&quot;onepiece(3)&quot;,&quot;onepiece(4)&quot;]\n",
    "<strong>解释：</strong>当创建最后一个文件夹时，最小的正有效 k 为 4 ，文件名变为 &quot;onepiece(4)&quot;。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [&quot;wano&quot;,&quot;wano&quot;,&quot;wano&quot;,&quot;wano&quot;]\n",
    "<strong>输出：</strong>[&quot;wano&quot;,&quot;wano(1)&quot;,&quot;wano(2)&quot;,&quot;wano(3)&quot;]\n",
    "<strong>解释：</strong>每次创建文件夹 &quot;wano&quot; 时，只需增加后缀中 k 的值即可。</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [&quot;kaido&quot;,&quot;kaido(1)&quot;,&quot;kaido&quot;,&quot;kaido(1)&quot;]\n",
    "<strong>输出：</strong>[&quot;kaido&quot;,&quot;kaido(1)&quot;,&quot;kaido(2)&quot;,&quot;kaido(1)(1)&quot;]\n",
    "<strong>解释：</strong>注意，如果含后缀文件名被占用，那么系统也会按规则在名称后添加新的后缀 (k) 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= names.length &lt;= 5 * 10^4</code></li>\n",
    "\t<li><code>1 &lt;= names[i].length &lt;= 20</code></li>\n",
    "\t<li><code>names[i]</code> 由小写英文字母、数字和/或圆括号组成。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [making-file-names-unique](https://leetcode.cn/problems/making-file-names-unique/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [making-file-names-unique](https://leetcode.cn/problems/making-file-names-unique/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"pes\",\"fifa\",\"gta\",\"pes(2019)\"]', '[\"gta\",\"gta(1)\",\"gta\",\"avalon\"]', '[\"onepiece\",\"onepiece(1)\",\"onepiece(2)\",\"onepiece(3)\",\"onepiece\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        hash = set()\n",
    "\n",
    "        for j in range(len(names)):\n",
    "            if names[j] in hash:\n",
    "                i = 1\n",
    "                while True:\n",
    "                    s = f\"{names[j]}({i})\"\n",
    "                    if s in hash:\n",
    "                        i += 1\n",
    "                    else:\n",
    "                        names[j] = s\n",
    "                        break\n",
    "            hash.add(names[j])\n",
    "\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        dic = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in dic:\n",
    "                k = dic[name]\n",
    "                while f'{name}({k})' in dic:\n",
    "                    k += 1\n",
    "                dic[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            dic[names[i]] = 1\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        \n",
    "        \n",
    "        nameindex = {}\n",
    "        n = len(names)\n",
    "        \n",
    "        for i,value in enumerate(names):\n",
    "            if value in nameindex.keys():\n",
    "                k = nameindex[value]\n",
    "                while f\"{value}({k})\" in nameindex:\n",
    "                    k += 1\n",
    "                tt = f\"{value}({k})\"\n",
    "                names[i] = tt\n",
    "                nameindex[value] = k + 1\n",
    "                \n",
    "                \n",
    "            \n",
    "            nameindex[names[i]] = 1\n",
    "\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\r\n",
    "        d = defaultdict(int)\r\n",
    "        for i, name in enumerate(names):\r\n",
    "            if name in d:\r\n",
    "                k = d[name]\r\n",
    "                while f'{name}({k})' in d:\r\n",
    "                    k += 1\r\n",
    "                d[name] = k + 1\r\n",
    "                names[i] = f'{name}({k})'\r\n",
    "            d[names[i]] = 1\r\n",
    "        return names\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        h = defaultdict(int)\n",
    "        for i, x in enumerate(names):\n",
    "            if x in h:\n",
    "                k = h[x]\n",
    "                while x+'('+str(k)+')' in h:\n",
    "                    k += 1\n",
    "                names[i] = x+'('+str(k)+')'\n",
    "                h[x] = k+1\n",
    "            h[names[i]] = 1\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        \n",
    "        nameindex = {}\n",
    "        n = len(names)\n",
    "        \n",
    "        for i,value in enumerate(names):\n",
    "            if value in nameindex:\n",
    "                k = nameindex[value]\n",
    "                #\"%s(%d)\" % (fn, cnt)  names[i]+'('+str(k)+')'  f\"{name}({k})\"\n",
    "                while f\"{value}({k})\" in nameindex:\n",
    "                    k += 1\n",
    "                tt = f\"{value}({k})\"\n",
    "                names[i] = tt\n",
    "                nameindex[value] = k+1\n",
    "                                           \n",
    "            nameindex[names[i]] = 1\n",
    "\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = defaultdict(int)\n",
    "        for i, name in enumerate(names):\n",
    "            if name in d:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k + 1\n",
    "                names[i] = f'{name}({k})'\n",
    "            d[names[i]] = 1\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        cnt = {}\n",
    "        for i in range(len(names)):\n",
    "            s = names[i]\n",
    "            while s in cnt:\n",
    "                s = f\"{names[i]}({cnt[names[i]]})\"\n",
    "                cnt[names[i]] += 1\n",
    "            cnt[s] = 1\n",
    "            names[i] = s\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        name_used = set()\n",
    "        ans = list()\n",
    "        for name in names:\n",
    "            if name not in name_used:\n",
    "                ans.append(name)\n",
    "                name_used.add(name)\n",
    "            else:\n",
    "                num = 1\n",
    "                nxt_name = f\"{name}({num})\"\n",
    "                while(nxt_name in name_used):\n",
    "                    num += 1\n",
    "                    nxt_name = f\"{name}({num})\"\n",
    "                name_used.add(nxt_name)\n",
    "                ans.append(nxt_name)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        result = list()\n",
    "        name_set = set()\n",
    "        for name in names:\n",
    "            if name in result:\n",
    "                cnt = 1\n",
    "                new_name = \"%s(%d)\" % (name, cnt)\n",
    "                while new_name in name_set:\n",
    "                    cnt += 1\n",
    "                    new_name = \"%s(%d)\" % (name, cnt)\n",
    "                result.append(new_name)\n",
    "                name_set.add(new_name)\n",
    "            else:\n",
    "                result.append(name)\n",
    "                name_set.add(name)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        result = list()\n",
    "        name_set = set()\n",
    "        for name in names:\n",
    "            if name in result:\n",
    "                cnt = 1\n",
    "                new_name = \"%s(%d)\" % (name, cnt)\n",
    "                while new_name in name_set:\n",
    "                    cnt += 1\n",
    "                    new_name = \"%s(%d)\" % (name, cnt)\n",
    "                result.append(new_name)\n",
    "                name_set.add(new_name)\n",
    "            else:\n",
    "                result.append(name)\n",
    "                name_set.add(name)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "import re\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\r\n",
    "        ret = []\r\n",
    "        hash = set()\r\n",
    "        for name in names:\r\n",
    "            if name in hash:\r\n",
    "                for i in range(1, len(ret) + 1):\r\n",
    "                    n = name + f\"({i})\"\r\n",
    "                    if n not in hash:\r\n",
    "                        ret.append(n)\r\n",
    "                        hash.add(n)\r\n",
    "                        break\r\n",
    "            else:\r\n",
    "                ret.append(name)\r\n",
    "                hash.add(name)\r\n",
    "        print(ret)\r\n",
    "        return ret\r\n",
    "        \r\n",
    "        \r\n",
    "        \r\n",
    "        \r\n",
    "        # ret = []\r\n",
    "        # hash = {}\r\n",
    "        # for i in names:\r\n",
    "        #     if i in hash.keys():\r\n",
    "        #         n = sorted(list(hash[i]))[0]\r\n",
    "        #         hash[i].discard(n)\r\n",
    "        #         hash[i].add(n + 1)\r\n",
    "        #         if n == 0:\r\n",
    "        #             m = i\r\n",
    "        #         else:\r\n",
    "        #             m = i+f'({n})'\r\n",
    "        #             hash[m] = hash.get(m,{0})\r\n",
    "        #             p = sorted(list(hash[m]))[0]\r\n",
    "        #             hash[m].discard(p)\r\n",
    "        #             hash[m].add(p + 1)\r\n",
    "        #         ret.append(m)\r\n",
    "        #     else:\r\n",
    "        #         ret.append(i)\r\n",
    "        #         hash[i] = {1}\r\n",
    "        #     re_ = re.match('(.*)\\((\\d+)\\)', i)\r\n",
    "        #     if re_:\r\n",
    "        #         name = re_.group(1)\r\n",
    "        #         index = int(re_.group(2))\r\n",
    "        #         if index == 0:\r\n",
    "        #             continue\r\n",
    "        #         # if index == 1:\r\n",
    "        #         #     t = [1]\r\n",
    "        #         # else:\r\n",
    "        #         #     t = [0, index]\r\n",
    "        #         # h = hash.get(name,0)\r\n",
    "        #         # if h:\r\n",
    "        #         #     if index not in h:\r\n",
    "        #         #         hash[name].append(index).sort()\r\n",
    "        #         # else:\r\n",
    "        #         #     hash[name] = t\r\n",
    "\r\n",
    "        #         hash[name] = hash.get(name, {0})\r\n",
    "        #         hash[name].discard(index)\r\n",
    "        #         hash[name].add(index + 1)\r\n",
    "        # # print(ret)\r\n",
    "        # # print(hash)\r\n",
    "        # return ret\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "import re\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\r\n",
    "        ret = []\r\n",
    "        hash = set()\r\n",
    "        for name in names:\r\n",
    "            if name in hash:\r\n",
    "                for i in range(1, len(names) + 1):\r\n",
    "                    n = name + f\"({i})\"\r\n",
    "                    if n not in hash:\r\n",
    "                        ret.append(n)\r\n",
    "                        hash.add(n)\r\n",
    "                        break\r\n",
    "            else:\r\n",
    "                ret.append(name)\r\n",
    "                hash.add(name)\r\n",
    "        print(ret)\r\n",
    "        return ret\r\n",
    "        \r\n",
    "        \r\n",
    "        \r\n",
    "        \r\n",
    "        # ret = []\r\n",
    "        # hash = {}\r\n",
    "        # for i in names:\r\n",
    "        #     if i in hash.keys():\r\n",
    "        #         n = sorted(list(hash[i]))[0]\r\n",
    "        #         hash[i].discard(n)\r\n",
    "        #         hash[i].add(n + 1)\r\n",
    "        #         if n == 0:\r\n",
    "        #             m = i\r\n",
    "        #         else:\r\n",
    "        #             m = i+f'({n})'\r\n",
    "        #             hash[m] = hash.get(m,{0})\r\n",
    "        #             p = sorted(list(hash[m]))[0]\r\n",
    "        #             hash[m].discard(p)\r\n",
    "        #             hash[m].add(p + 1)\r\n",
    "        #         ret.append(m)\r\n",
    "        #     else:\r\n",
    "        #         ret.append(i)\r\n",
    "        #         hash[i] = {1}\r\n",
    "        #     re_ = re.match('(.*)\\((\\d+)\\)', i)\r\n",
    "        #     if re_:\r\n",
    "        #         name = re_.group(1)\r\n",
    "        #         index = int(re_.group(2))\r\n",
    "        #         if index == 0:\r\n",
    "        #             continue\r\n",
    "        #         # if index == 1:\r\n",
    "        #         #     t = [1]\r\n",
    "        #         # else:\r\n",
    "        #         #     t = [0, index]\r\n",
    "        #         # h = hash.get(name,0)\r\n",
    "        #         # if h:\r\n",
    "        #         #     if index not in h:\r\n",
    "        #         #         hash[name].append(index).sort()\r\n",
    "        #         # else:\r\n",
    "        #         #     hash[name] = t\r\n",
    "\r\n",
    "        #         hash[name] = hash.get(name, {0})\r\n",
    "        #         hash[name].discard(index)\r\n",
    "        #         hash[name].add(index + 1)\r\n",
    "        # # print(ret)\r\n",
    "        # # print(hash)\r\n",
    "        # return ret\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        existName = defaultdict(int)\n",
    "        ans = []\n",
    "        for n in names:\n",
    "            if existName[n] > 0:\n",
    "                i,curName = existName[n],n+\"(\"+str(existName[n])+\")\"\n",
    "                while curName in existName:\n",
    "                    i += 1\n",
    "                    curName = n+\"(\"+str(i)+\")\"\n",
    "                existName[n] += 1\n",
    "            else:\n",
    "                curName = n\n",
    "            existName[curName] += 1\n",
    "            ans.append(curName)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        cnt = collections.defaultdict(int)\n",
    "        for name in names:\n",
    "            if name not in cnt:\n",
    "                ans.append(name)\n",
    "                cnt[name] = 1\n",
    "            else:\n",
    "                k = cnt[name]\n",
    "                while name + '(' + str(k) + ')' in cnt:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                cnt[name] = k + 1\n",
    "                cnt[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        hashmap = {}\n",
    "        ans = []\n",
    "        for name in names:\n",
    "            if name not in hashmap:\n",
    "                ans.append(name)\n",
    "                hashmap[name] = 1\n",
    "            else:\n",
    "                k = hashmap[name]\n",
    "                while (t := name + f\"({k})\") in hashmap:\n",
    "                    k += 1\n",
    "                ans.append(t)\n",
    "                hashmap[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        file = {} #字典，承载文件名称和对应的出现次数\n",
    "        ans = []#最后用来装答案的列表\n",
    "\n",
    "        for name in names:\n",
    "            if name not in file:\n",
    "                file[name] = 1\n",
    "                ans.append(name)\n",
    "            else:\n",
    "                k = file[name] #用来记录这个文件在原始输入中已经输入了几次，只关注没有自带括号的那种。\n",
    "                while name + '(' + str(k) + ')' in file:\n",
    "                    k += 1\n",
    "                file[name] = k+1 #在字典中记录这个文件目前新的一轮应该多加一次出现次数\n",
    "                new_name = name + '(' + str(k) + ')'\n",
    "                ans.append(new_name)\n",
    "                file[new_name] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        visit = defaultdict(int, default=0)\n",
    "        ret = []\n",
    "        for name in names:\n",
    "            if name in visit:\n",
    "                while name + f'({visit[name]})' in visit:\n",
    "                    visit[name] += 1\n",
    "                name += f'({visit[name]})'\n",
    "            ret.append(name)\n",
    "            visit[name] += 1\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        dic = collections.defaultdict(int)\n",
    "        for val in names:\n",
    "            if dic[val] == 0:\n",
    "                res.append(val)   \n",
    "            else:\n",
    "                k = dic[val]\n",
    "                while True:\n",
    "                    temp = val + str('(') + str(k) + str(')')\n",
    "                    if dic[temp] == 0:\n",
    "                        res.append(temp)\n",
    "                        dic[temp] = 1\n",
    "                        break\n",
    "                    k += 1\n",
    "            dic[val] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while f'{name}({k})' in index:\n",
    "                    k += 1\n",
    "                t = f'{name}({k})'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for name in names:\n",
    "            if name not in dic:\n",
    "                res.append(name)\n",
    "                dic[name] = 1\n",
    "            else:\n",
    "                #print(dic)\n",
    "                i = dic[name]\n",
    "                while f'{name}({i})' in dic:\n",
    "                    i += 1\n",
    "                res.append(f'{name}({i})')\n",
    "                dic[name] += 1\n",
    "                dic[res[-1]] = 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        hashmap = {}\n",
    "        res = []\n",
    "\n",
    "        for name in names:\n",
    "            if name not in hashmap:\n",
    "                res.append(name)\n",
    "                hashmap[name] = 1\n",
    "            else:\n",
    "                times = hashmap[name]\n",
    "                yuan = name\n",
    "                name = yuan + \"(\" + str(times) + ')'\n",
    "                while name in hashmap:\n",
    "                    times += 1\n",
    "                    name = yuan + \"(\" + str(times) + ')'\n",
    "                hashmap[yuan] = times+1\n",
    "                hashmap[name] = 1\n",
    "                res.append(name)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        name_used = dict()\n",
    "        ans = list()\n",
    "        for name in names:         # 遍历names数组\n",
    "            if name not in name_used:       # 若name不位于哈希表name_used中，\n",
    "                ans.append(name)            # 可以放心地将name加入ans\n",
    "                name_used[name] = 1         # 同时要初始化k-v pair\n",
    "                # value为1表示，下一个以key为前缀的文件名的后缀括号中的数字应该为1\n",
    "                # 假设name = pes首次出现，那么下一次遇到pes，则应该得到pes(1)\n",
    "            else:                   # 若name位于哈希表name_used中，说明需要添加后缀\n",
    "            # 后缀的数字最小为num，但不一定是num，因为可能num已经被用了\n",
    "            # 先根据num获得nxt_name，然后循环判断nxt_name是否位于哈希表name_used中\n",
    "            # 以示例2为例：遍历到下标为2的'gta'时，因为'gta(1)'已经被使用，\n",
    "            # 所以需要找到'gta(2)'中的这个2\n",
    "                num = name_used[name]           \n",
    "                nxt_name = f\"{name}({num})\"\n",
    "                # 若nxt_name已经位于name_used中，那么num必须增大，\n",
    "                # 直到对应的nxt_name不位于name_used中，此时的nxt_name才是正确的文件名\n",
    "                while(nxt_name in name_used):           \n",
    "                    num += 1\n",
    "                    nxt_name = f\"{name}({num})\"\n",
    "                # 得到nxt_name后，需要做三个修改\n",
    "                name_used[nxt_name] = 1         # 1.把nxt_name加入哈希表，其value为1\n",
    "                name_used[name] = num+1         # 2.修改name对应的下标，之后再遇到name就应该从num+1开始找后缀了\n",
    "                ans.append(nxt_name)            # 3.nxt_name为当前name对应的文件名，应该加入ans中\n",
    "        return ans  # 最后返回ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        dic = collections.defaultdict(int)\n",
    "        for val in names:\n",
    "            if dic[val] == 0:\n",
    "                res.append(val)\n",
    "                \n",
    "            else:\n",
    "                k = dic[val]\n",
    "                while True:\n",
    "                    temp = val + str('(') + str(k) + str(')')\n",
    "                    if dic[temp] == 0:\n",
    "                        res.append(temp)\n",
    "                        dic[temp] = 1\n",
    "                        break\n",
    "                    k += 1\n",
    "            dic[val] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans=[]\n",
    "        check=dict()\n",
    "        for x in names:\n",
    "            if x not in check:\n",
    "                ans.append(x)\n",
    "                check[x]=1\n",
    "            else:\n",
    "                idx=check[x]\n",
    "                flag=False\n",
    "                while x+\"(\"+str(idx)+\")\" in check:\n",
    "                    idx+=1\n",
    "                    flag=True\n",
    "                cur=x+\"(\"+str(idx)+\")\"\n",
    "                ans.append(cur)\n",
    "                check[cur]=1\n",
    "                if not flag: idx+=1\n",
    "                check[x]=idx\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        used_names = {}  # 存储已使用的文件名和它们的计数\n",
    "\n",
    "        result = []  # 存储最终结果\n",
    "\n",
    "        for name in names:\n",
    "            if name not in used_names:\n",
    "                result.append(name)\n",
    "                used_names[name] = 1\n",
    "            else:\n",
    "                k = used_names[name]  # 获取当前文件名的计数\n",
    "                new_name = name + f'({k})'  # 根据计数生成新的文件名\n",
    "                while new_name in used_names:\n",
    "                    k += 1\n",
    "                    new_name = name + f'({k})'\n",
    "                result.append(new_name)\n",
    "                used_names[new_name] = 1\n",
    "                used_names[name] = k + 1\n",
    "\n",
    "        return result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        name_used = dict()\n",
    "        ans = list()\n",
    "        for name in names:         # 遍历names数组\n",
    "            if name not in name_used:       # 若name不位于哈希表name_used中，\n",
    "                ans.append(name)            # 可以放心地将name加入ans\n",
    "                name_used[name] = 1         # 同时要初始化k-v pair\n",
    "                # value为1表示，下一个以key为前缀的文件名的后缀括号中的数字应该为1\n",
    "                # 假设name = pes首次出现，那么下一次遇到pes，则应该得到pes(1)\n",
    "            else:                   # 若name位于哈希表name_used中，说明需要添加后缀\n",
    "            # 后缀的数字最小为num，但不一定是num，因为可能num已经被用了\n",
    "            # 先根据num获得nxt_name，然后循环判断nxt_name是否位于哈希表name_used中\n",
    "            # 以示例2为例：遍历到下标为2的'gta'时，因为'gta(1)'已经被使用，\n",
    "            # 所以需要找到'gta(2)'中的这个2\n",
    "                num = name_used[name]           \n",
    "                nxt_name = f\"{name}({num})\"\n",
    "                # 若nxt_name已经位于name_used中，那么num必须增大，\n",
    "                # 直到对应的nxt_name不位于name_used中，此时的nxt_name才是正确的文件名\n",
    "                while(nxt_name in name_used):           \n",
    "                    num += 1\n",
    "                    nxt_name = f\"{name}({num})\"\n",
    "                # 得到nxt_name后，需要做三个修改\n",
    "                name_used[nxt_name] = 1         # 1.把nxt_name加入哈希表，其value为1\n",
    "                name_used[name] = num+1         # 2.修改name对应的下标，之后再遇到name就应该从num+1开始找后缀了\n",
    "                ans.append(nxt_name)            # 3.nxt_name为当前name对应的文件名，应该加入ans中\n",
    "        return ans  # 最后返回ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for name in names:\n",
    "            if name not in dic:\n",
    "                res.append(name)\n",
    "                dic[name] = 1\n",
    "            else:\n",
    "                #print(dic)\n",
    "                i = dic[name]\n",
    "                while f'{name}({i})' in dic:\n",
    "                    i += 1\n",
    "                res.append(f'{name}({i})')\n",
    "                dic[name] = i+1\n",
    "                dic[res[-1]] = 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        counter = defaultdict(int)\n",
    "        result = list()\n",
    "        for name in names:\n",
    "            # print(name, counter)\n",
    "            if name in counter:\n",
    "                tmp_name = f\"{name}({counter[name]})\"\n",
    "                while tmp_name in counter:\n",
    "                    # print(tmp_name)\n",
    "                    counter[name] += 1\n",
    "                    tmp_name = f\"{name}({counter[name]})\"\n",
    "                # print(tmp_name)\n",
    "                result.append(tmp_name)\n",
    "                counter[tmp_name] += 1\n",
    "            else:\n",
    "                result.append(name)\n",
    "                counter[name] += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        \n",
    "        d, ans = {}, []\n",
    "        for name in names:\n",
    "            s = name\n",
    "            while s in d:\n",
    "                s = f'{name}({d[name]})'\n",
    "                d[name] += 1\n",
    "            d[s] = 1\n",
    "            ans.append(s)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while f'{name}({k})' in index:\n",
    "                    k += 1\n",
    "                ans.append(t := f'{name}({k})')\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        dic = collections.defaultdict(int)\n",
    "        for val in names:\n",
    "            if dic[val] == 0:\n",
    "                res.append(val)   \n",
    "            else:\n",
    "                k = dic[val]\n",
    "                while True:\n",
    "                    temp = val + str('(') + str(k) + str(')')\n",
    "                    if dic[temp] == 0:\n",
    "                        res.append(temp)\n",
    "                        dic[temp] = 1\n",
    "                        break\n",
    "                    k += 1\n",
    "            dic[val] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        cnt = collections.defaultdict(int)\n",
    "        for name in names:\n",
    "            if name not in cnt:\n",
    "                ans.append(name)\n",
    "                cnt[name] = 1\n",
    "            else:\n",
    "                k = cnt[name]\n",
    "                while name + '(' + str(k) + ')' in cnt:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                cnt[name] = k + 1\n",
    "                cnt[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        mp = collections.defaultdict(int)\n",
    "        ans = []\n",
    "        for name in names:\n",
    "            k = 1\n",
    "            if mp[name] == 0:\n",
    "                ans.append(name)\n",
    "                mp[name] = 1\n",
    "            else:\n",
    "                t = f\"{name}({k})\"\n",
    "                while mp[t] != 0:\n",
    "                    k += 1\n",
    "                    t = f\"{name}({k})\"\n",
    "                mp[t] = 1\n",
    "                ans.append(t)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans=[]\n",
    "        check=dict()\n",
    "        for x in names:\n",
    "            if x not in check:\n",
    "                ans.append(x)\n",
    "                check[x]=1\n",
    "            else:\n",
    "                idx=check[x]\n",
    "                flag=False\n",
    "                while x+\"(\"+str(idx)+\")\" in check:\n",
    "                    idx+=1\n",
    "                    flag=True\n",
    "                cur=x+\"(\"+str(idx)+\")\"\n",
    "                ans.append(cur)\n",
    "                check[cur]=1\n",
    "                if not flag: idx+=1\n",
    "                check[x]=idx\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        result_list = []\n",
    "        repeat_count = {}\n",
    "        for name in names:\n",
    "            if name not in repeat_count:\n",
    "                repeat_count[name] = 1\n",
    "                result_list.append(name)\n",
    "            else:\n",
    "                count = repeat_count[name]\n",
    "                temp = name + '('+str(count)+')'\n",
    "                while temp in repeat_count:\n",
    "                    count += 1\n",
    "                    temp = name + '('+str(count)+')'\n",
    "                \n",
    "                repeat_count[name] = count+1\n",
    "                repeat_count[temp] = 1\n",
    "                \n",
    "                result_list.append(temp)\n",
    "        \n",
    "        return result_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        index = {}\n",
    "        for name in names:\n",
    "            if name not in index:\n",
    "                ans.append(name)\n",
    "                index[name] = 1\n",
    "            else:\n",
    "                k = index[name]\n",
    "                while name + '(' + str(k) + ')' in index:\n",
    "                    k += 1\n",
    "                    \n",
    "                t = name + '(' + str(k) + ')'\n",
    "                ans.append(t)\n",
    "                index[name] = k + 1\n",
    "                index[t] = 1\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        map_dict = {}\n",
    "        for name in names:\n",
    "            if name not in map_dict:\n",
    "                map_dict[name] = True\n",
    "            else:\n",
    "                idx = 1\n",
    "                while True:\n",
    "                    if name + f\"({idx})\" not in map_dict:\n",
    "                        map_dict[name + f\"({idx})\"] = True\n",
    "                        break\n",
    "                    else:\n",
    "                        idx+=1\n",
    "        return list(map_dict.keys())\n",
    "                    \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        map_dict = {}\n",
    "        for name in names:\n",
    "            if name not in map_dict:\n",
    "                map_dict[name] = True\n",
    "            else:\n",
    "                idx = 1\n",
    "                while name + f\"({idx})\" in map_dict:\n",
    "                    idx+=1\n",
    "                map_dict[name + f\"({idx})\"] = True\n",
    "        return list(map_dict.keys())\n",
    "                    \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        name_used = dict()\n",
    "        ans = list()\n",
    "        for name in names:         # 遍历names数组\n",
    "            if name not in name_used:       # 若name不位于哈希表name_used中，\n",
    "                ans.append(name)            # 可以放心地将name加入ans\n",
    "                name_used[name] = 1         # 同时要初始化k-v pair\n",
    "                # value为1表示，下一个以key为前缀的文件名的后缀括号中的数字应该为1\n",
    "                # 假设name = pes首次出现，那么下一次遇到pes，则应该得到pes(1)\n",
    "            else:                   # 若name位于哈希表name_used中，说明需要添加后缀\n",
    "            # 后缀的数字最小为num，但不一定是num，因为可能num已经被用了\n",
    "            # 先根据num获得nxt_name，然后循环判断nxt_name是否位于哈希表name_used中\n",
    "            # 以示例2为例：遍历到下标为2的'gta'时，因为'gta(1)'已经被使用，\n",
    "            # 所以需要找到'gta(2)'中的这个2\n",
    "                num = name_used[name]           \n",
    "                nxt_name = f\"{name}({num})\"\n",
    "                # 若nxt_name已经位于name_used中，那么num必须增大，\n",
    "                # 直到对应的nxt_name不位于name_used中，此时的nxt_name才是正确的文件名\n",
    "                while(nxt_name in name_used):           \n",
    "                    num += 1\n",
    "                    nxt_name = f\"{name}({num})\"\n",
    "                # 得到nxt_name后，需要做三个修改\n",
    "                name_used[nxt_name] = 1         # 1.把nxt_name加入哈希表，其value为1\n",
    "                name_used[name] = num+1         # 2.修改name对应的下标，之后再遇到name就应该从num+1开始找后缀了\n",
    "                ans.append(nxt_name)            # 3.nxt_name为当前name对应的文件名，应该加入ans中\n",
    "        return ans  # 最后返回ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        visit = defaultdict(int, default=0)\n",
    "        ret = []\n",
    "        for name in names:\n",
    "            if name in visit:\n",
    "                while name + f'({visit[name]})' in visit:\n",
    "                    visit[name] += 1\n",
    "                name += f'({visit[name]})'\n",
    "            ret.append(name)\n",
    "            visit[name] = 1\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        m={}\n",
    "        res=[]\n",
    "        for x in names:\n",
    "            if x not in m:\n",
    "                m[x]=0\n",
    "                res.append(x)\n",
    "            else:\n",
    "                i=m[x]+1\n",
    "                s=x+f'({i})'\n",
    "                while s in m:\n",
    "                    i+=1\n",
    "                    s=x+f'({i})'\n",
    "                m[x]=i\n",
    "                m[s]=0\n",
    "                res.append(s)\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        #hist = []\n",
    "        hist_idx_map = {}\n",
    "        ans_names = []\n",
    "        for name in names:\n",
    "            if not name in hist_idx_map:\n",
    "                ans_names.append(name)\n",
    "                hist_idx_map[name] = 1\n",
    "                #hist.append(name)\n",
    "            else:\n",
    "                #i = 1\n",
    "                i = hist_idx_map.get(name,1)\n",
    "                #i += 1\n",
    "                while True:\n",
    "                    new_name = f'{name}({i})'\n",
    "                    if not new_name in hist_idx_map:\n",
    "                        ans_names.append(new_name)\n",
    "                        #hist.append(new_name)\n",
    "                        hist_idx_map[name] = i\n",
    "                        hist_idx_map[new_name] = 1\n",
    "                        break\n",
    "                    i += 1\n",
    "        #print(re.findall('\\(\\d*\\)','pes(1234)(2019)'))\n",
    "        return ans_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        def addSuffix(name: str, k: int) -> str:\n",
    "            return f\"{name}({k})\"\n",
    "        n = len(names)\n",
    "        map = {}\n",
    "        res = [''] * n\n",
    "        for i in range(n):\n",
    "            name = names[i]\n",
    "            if name in map:\n",
    "                k = map[name]\n",
    "                # 包含了，递增k\n",
    "                while addSuffix(name, k) in map:\n",
    "                    k+=1\n",
    "                res[i] = addSuffix(name, k)\n",
    "                # 更新下标\n",
    "                map[name] = k+1\n",
    "                # 初始化新字符的下一个下标\n",
    "                map[res[i]] = 1\n",
    "            else:\n",
    "                # 不存在\n",
    "                res[i] = name\n",
    "                # 初始化新字符的下一个下标\n",
    "                map[name] = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSuffix(self, name: str, k: int) -> str:\n",
    "            return f\"{name}({k})\"\n",
    "\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        n = len(names)\n",
    "        map = {}\n",
    "        res = [''] * n\n",
    "        for i in range(n):\n",
    "            name = names[i]\n",
    "            if name in map:\n",
    "                k = map[name]\n",
    "                # 包含了，递增k\n",
    "                while self.addSuffix(name, k) in map:\n",
    "                    k+=1\n",
    "                res[i] = self.addSuffix(name, k)\n",
    "                # 更新下标\n",
    "                map[name] = k+1\n",
    "                # 初始化新字符的下一个下标\n",
    "                map[res[i]] = 1\n",
    "            else:\n",
    "                # 不存在\n",
    "                res[i] = name\n",
    "                # 初始化新字符的下一个下标\n",
    "                map[name] = 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        m=set()\n",
    "        res=[]\n",
    "        for x in names:\n",
    "            if x not in m:\n",
    "                m.add(x)\n",
    "                res.append(x)\n",
    "            else:\n",
    "                i=1\n",
    "                while x+f'({i})' in m:\n",
    "                    i+=1\n",
    "                m.add(x+f'({i})')\n",
    "                res.append(x+f'({i})')\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:  \n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:  \n",
    "        seen = set()  \n",
    "        result = []  \n",
    "        for name in names:  \n",
    "            if name not in seen:  \n",
    "                result.append(name)  \n",
    "                seen.add(name)  \n",
    "            else:  \n",
    "                k = 1  \n",
    "                while f\"{name}({k})\" in seen:  \n",
    "                    k += 1  \n",
    "                result.append(f\"{name}({k})\")  \n",
    "                seen.add(f\"{name}({k})\")  \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ha = defaultdict()\n",
    "        res = list()\n",
    "        for s in names:\n",
    "            if s in ha:\n",
    "                new_s = s+'({})'.format(ha[s])\n",
    "                while new_s in ha:\n",
    "                    ha[s] += 1\n",
    "                    new_s = s+'({})'.format(ha[s])\n",
    "                res.append(s+'({})'.format(ha[s]))\n",
    "                ha[s] += 1\n",
    "                ha[new_s] = 1\n",
    "            else:\n",
    "                ha[s] = 1\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ha = defaultdict()\n",
    "        res = list()\n",
    "        for s in names:\n",
    "            if s in ha:\n",
    "                new_s = s+'({})'.format(ha[s])\n",
    "                while new_s in ha:\n",
    "                    ha[s] += 1\n",
    "                    new_s = s+'({})'.format(ha[s])\n",
    "                res.append(s+'({})'.format(ha[s]))\n",
    "                ha[s] += 1\n",
    "                ha[new_s] = 1\n",
    "            else:\n",
    "                ha[s] = 1\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ha = defaultdict()\n",
    "        res = list()\n",
    "        for s in names:\n",
    "            if s in ha:\n",
    "                new_s = s+'({})'.format(ha[s])\n",
    "                while new_s in ha:\n",
    "                    ha[s] += 1\n",
    "                    new_s = s+'({})'.format(ha[s])\n",
    "                res.append(s+'({})'.format(ha[s]))\n",
    "                ha[s] += 1\n",
    "                ha[new_s] = 1\n",
    "            else:\n",
    "                ha[s] = 1\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        list = []\n",
    "        dict = {}\n",
    "        for i in names:\n",
    "            if i not in dict:\n",
    "                list.append(i)\n",
    "                dict[i] = 1\n",
    "            else:\n",
    "                k = dict[i]\n",
    "                while i + '('+ str(k)+')'in dict:\n",
    "                    k+=1\n",
    "                list.append(i + '('+ str(k)+')')\n",
    "                dict[i] = k+1\n",
    "                dict[i + '('+ str(k)+')'] = 1\n",
    "        return list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        cnt = Counter()\n",
    "        ans = []\n",
    "        for s in names:\n",
    "            if s not in cnt:\n",
    "                cnt[s] = 1\n",
    "                ans.append(s)\n",
    "            else:\n",
    "                k = cnt[s]\n",
    "                while s + \"(\" + str(k) + \")\" in cnt:\n",
    "                    k += 1\n",
    "                ans.append(s + \"(\" + str(k) + \")\")\n",
    "                cnt[s] = k + 1\n",
    "                cnt[s + \"(\" + str(k) + \")\"] = 1\n",
    "        return ans      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        d = {}\n",
    "        res = []\n",
    "        for name in names:\n",
    "            if name not in d:\n",
    "                d[name] = 1\n",
    "                res.append(name)\n",
    "            else:\n",
    "                k = d[name]\n",
    "                while f'{name}({k})' in d:\n",
    "                    k += 1\n",
    "                d[name] = k\n",
    "                d[f'{name}({k})'] = 1\n",
    "                res.append(f'{name}({k})')\n",
    "        return res\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        visit = defaultdict(int, default=0)\n",
    "        ret = []\n",
    "        for name in names:\n",
    "            if name not in visit:\n",
    "                ret.append(name)\n",
    "                visit[name] += 1\n",
    "                \"\"\"if name[-1] == ')':\n",
    "                    left = 0\n",
    "                    for right in range(len(name)-1, -1, -1):\n",
    "                        if name[right] == '(':\n",
    "                            left = right\n",
    "                            break\n",
    "                    visit[name[:right]] = max(visit[name[:right]], int(name[right+1:-1]))\"\"\"\n",
    "            else:\n",
    "                while name + f'({visit[name]})' in visit:\n",
    "                    visit[name] += 1\n",
    "                ret.append(name + f'({visit[name]})')\n",
    "                visit[name + f'({visit[name]})'] += 1\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        dict1 = dict()    # 记录每个文件的最小编号\n",
    "        len_names = len(names)\n",
    "        for i in range(len_names):\n",
    "            if names[i] not in dict1:\n",
    "                ans.append(names[i])\n",
    "                dict1[names[i]] = 1\n",
    "            else:\n",
    "                k = dict1[names[i]]\n",
    "                # 如果字符串已经在字典里，\n",
    "                while f'{names[i]}({k})' in dict1:\n",
    "                    k += 1\n",
    "                dict1[f'{names[i]}({k})'] = 1\n",
    "                dict1[names[i]] += 1\n",
    "                ans.append(f'{names[i]}({k})')\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ha = defaultdict()\n",
    "        res = list()\n",
    "        for s in names:\n",
    "            if s in ha:\n",
    "                new_s = s+'({})'.format(ha[s])\n",
    "                while new_s in ha:\n",
    "                    ha[s] += 1\n",
    "                    new_s = s+'({})'.format(ha[s])\n",
    "                res.append(s+'({})'.format(ha[s]))\n",
    "                ha[s] += 1\n",
    "                ha[new_s] = 1\n",
    "            else:\n",
    "                ha[s] = 1\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ha = defaultdict()\n",
    "        res = list()\n",
    "        for s in names:\n",
    "            if s in ha:\n",
    "                new_s = s+'({})'.format(ha[s])\n",
    "                while new_s in ha:\n",
    "                    ha[s] += 1\n",
    "                    new_s = s+'({})'.format(ha[s])\n",
    "                res.append(s+'({})'.format(ha[s]))\n",
    "                ha[s] += 1\n",
    "                ha[new_s] = 1\n",
    "            else:\n",
    "                ha[s] = 1\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        mapping = {}\n",
    "        result = []\n",
    "        for name in names:\n",
    "            if(name not in mapping):\n",
    "                mapping[name] = 1\n",
    "                result.append(name)\n",
    "            else:\n",
    "                i = mapping[name]\n",
    "                while(name + f'({i})' in mapping):\n",
    "                    i += 1\n",
    "                mapping[name] = i + 1\n",
    "                mapping[name+f\"({i})\"] = 1\n",
    "                result.append(name+f\"({i})\")\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        dict1 = dict()    # 记录每个文件的最小编号\n",
    "        len_names = len(names)\n",
    "        for i in range(len_names):\n",
    "            if names[i] not in dict1:\n",
    "                ans.append(names[i])\n",
    "                dict1[names[i]] = 1\n",
    "            else:\n",
    "                k = dict1[names[i]]\n",
    "                # 如果字符串已经在字典里，\n",
    "                while f'{names[i]}({k})' in dict1:\n",
    "                    k += 1\n",
    "                dict1[f'{names[i]}({k})'] = 1\n",
    "                ans.append(f'{names[i]}({k})')\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getFolderNames(self, names: List[str]) -> List[str]:\n",
    "        ans = list()\n",
    "        d = dict()\n",
    "        \n",
    "        for i in range(len(names)):\n",
    "            if names[i] not in d:\n",
    "                d[names[i]] = 1\n",
    "                ans.append(names[i])\n",
    "            else:\n",
    "                for j in range(d[names[i]], len(names)):\n",
    "                    if names[i] + '('+str(j)+')' not in d:\n",
    "                        d[names[i]+ '('+str(j)+')'] = 1\n",
    "                        d[names[i]] += 1\n",
    "                        ans.append(names[i]+ '('+str(j)+')')\n",
    "                        break\n",
    "\n",
    "        return ans \n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
